#include "GameState.h"
#include "Game.h"
#include "SDL_OpenGl.h"
#include <cassert>
#include <gl/glut.h>
/* An extension used to manually control the V sync rate */
typedef BOOL (APIENTRY *PFNWGLSWAPINTERVALFARPROC)( int );
PFNWGLSWAPINTERVALFARPROC wglSwapIntervalEXT = 0;
/* local function, set interval to 0 to not wait for v sync */
void GameState::SetVsync(int interval)
{
  const char *extensions = (const char*)glGetString( GL_EXTENSIONS );

  if( strstr( extensions, "WGL_EXT_swap_control" ) == 0 )
    return; // Error: WGL_EXT_swap_control extension not supported on your computer.\n");
  else
  {
    wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress( "wglSwapIntervalEXT" );

    if( wglSwapIntervalEXT )
      wglSwapIntervalEXT(interval);
  }
}
void GameState::ChangeState(GameState* gs)
{
	//base implementation.
	 TheGame::Instance()->RequestChange(gs);
}
//Rubbish! Redo this!
float GameState::Random(float min, float max)
{
    float range = max - min;
    if(range < 1)
        range = 1.0f;
    int num = rand() % (int)range;
    return( num + min );
}
void GameState::CheckErrors()
{
    char* c = SDL_GetError();
    int x = glGetError();
    if(x == GL_INVALID_ENUM || x == GL_INVALID_VALUE ||x == GL_INVALID_OPERATION || 
        x == GL_STACK_OVERFLOW || x == GL_STACK_UNDERFLOW || x == GL_OUT_OF_MEMORY )
        assert(0);
}
void GameState::RenderText(float x, float y, const std::string& s, float r, float g, float b)
{
    glColor3f(r,g,b);
    glRasterPos2f(x,y);
    for (unsigned int i = 0; i < s.size() ; ++i)
        glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, s[i]);
}
